package com.hxtec.polaris.com.hxtec.polaris.insertDb;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hxtec.polaris.entity.ShopCategory;
import com.hxtec.polaris.entity.ShopProductBase;
import com.hxtec.polaris.mapper.ShopCategoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * @Describtion 分类json入库
 * @Author yonyong
 * @Date 2019/11/11 16:47
 * @Version 1.0.0
 **/
@SpringBootTest
public class InsertCategory {

    @Resource
    private ShopCategoryMapper shopCategoryMapper;
    /**
     * 读取json文件
     */
    public static String readJsonData(String filePath){
        URL url= InsertCategory.class.getClassLoader().getResource(filePath);
        assert url != null;
        String path = url.getPath();
        File jsonFile = new File(path);
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 执行此方法    入库相关表 shop_category
     * @throws Exception
     */
    @Test
    public void insert() throws Exception {
        final ObjectMapper mapper = new ObjectMapper();
        String json = readJsonData("json/category.json");
        List list = mapper.readValue(json,List.class);
        for (Object o :list){
            HashMap map = (HashMap) o;
            ShopCategory shopCategory = new ShopCategory();

            String id = String.valueOf( map.get("id"));
            String pid = String.valueOf( map.get("pid"));
            String name = String.valueOf( map.get("name"));
            String picture = String.valueOf( map.get("picture"));

            if (null!=id && !id.equals("null")){
                shopCategory.setId(Integer.valueOf(id));
            }
            if (null!=pid && !pid.equals("null")){
                shopCategory.setParentId(pid);
            }
            if (null!=name && !name.equals("null")){
                shopCategory.setName(name);
            }
            if (null!=picture && !picture.equals("null")){
                shopCategory.setImage(picture);
            }

            shopCategory.setIsParent("N");
            shopCategory.setIsDelete("N");
            shopCategory.setCreateTime(new Date());
            shopCategory.setUpdateTime(new Date());

            shopCategoryMapper.insert(shopCategory);
        }

        //修正数据库is_parent状态
        updateIsParent();
        //构建分类路径
        buildPath();

    }

    /**
     * 修正数据库is_parent状态
     */
    @Test
    public void updateIsParent(){
        List<ShopCategory> list = shopCategoryMapper.selectAll();
        for (ShopCategory s: list){
            if (StringUtils.isNotBlank(s.getParentId())){
                ShopCategory temp = shopCategoryMapper.selectByPrimaryKey(s.getParentId());
                temp.setIsParent("Y");
                shopCategoryMapper.updateByPrimaryKeySelective(temp);
            }
        }
    }

    @Test
    public void read(){
        System.out.println(readJsonData("json/category.json"));
    }

    /**
     * 构建分类路径
     */
    @Test
    public void buildPath(){
        List<ShopCategory> list = shopCategoryMapper.selectAll();
        for (ShopCategory shopCategory:list){
            //获取构建的类别路径
//            StringBuilder path = new StringBuilder(shopCategory.getId());
            StringBuilder path = new StringBuilder("-");
            path.append(shopCategory.getId());
            System.out.println("start *****"+getPath(shopCategory,path));
            path.append("-");
            System.out.println("end *****"+path);
            shopCategory.setComment1(String.valueOf(path));
            shopCategoryMapper.updateByPrimaryKeySelective(shopCategory);
        }
    }

    //获取某个分类的路径
    private StringBuilder getPath(ShopCategory shopCategory,StringBuilder path){
        if (StringUtils.isNotBlank(shopCategory.getParentId())){
            Example example = new Example(ShopCategory.class);
            example.createCriteria().andEqualTo("id",shopCategory.getParentId());
            List<ShopCategory> list= shopCategoryMapper.selectByExample(example);
            if (null != list && list.size()>0){
                ShopCategory shopCategory1 = list.get(0);
                path.append("-").append(shopCategory1.getId());

                if (StringUtils.isNotBlank(shopCategory1.getParentId())){
                    getPath(shopCategory1,path);
                }
            }
        }
        return path;
    }

    @Test
    public void test(){
        String value = format("demo-pool-%d", 0);
        System.out.println(value);
    }
    private static String format(String format, Object... args) {
        return String.format(Locale.ROOT, format, args);
    }
}
